{php $bootstrap_type = 3;}
{template 'header'}
{template 'common'}

<link type="text/css" rel="stylesheet" href="../addons/ewei_shopping/images/style.css?{TIMESTAMP}">

<div class="head">
	<a href="javascript:history.back();" class="bn pull-left"><i class="fa fa-angle-left"></i></a>
	<span class="title">收货地址</span>
	<a href="javascript:;" class="bn pull-right"><i class="fa fa-shopping-cart"></i><span class="buy-num img-circle"> {$carttotal} </span></a>
</div>

<form class="form-horizontal" method="post" role="form">
	<input type="hidden" name="goodstype" value="{$goodstype}" />
	<div class="order-main">
		<div id="myaddress">
		{loop $addresses $row}
			<div id='address_{$row['id']}' class="{if $row['isdefault']==1}shopcart-main1{else}shopcart-main{/if} img-rounded address_item" style='margin:0;padding:10px;margin-bottom:10px;cursor:pointer' onclick='editAddress({$row['id']},{php echo $row['isdefault']==1?'1':'0'})'>
				<span>{$row['province']} {$row['city']} {$row['district']} {$row['address']} <br/> {$row['username']} - {$row['mobile']}</span><br/>
				<span>
					{if !$row['isdefault']}<a href="javascript:;" onclick="saveDefaultAddress(event,{$row['id']})">设为默认</a>{/if}
					<a href="javascript:;" onclick="removeAddress(event,{$row['id']})"><i class="fa fa-remove"></i> 删除</a>
				</span>
			</div>
		{/loop}
		</div>
		<div><button type="button" class="btn btn-danger" onclick="addAddress()"><i class="fa fa-plus"></i> 添加修改地址</button></div>
		<div class="add-address img-rounded" id="addAddressPanel" {if !empty($addresses)} style="display:none;"{/if}>
			<div class="add-address-hd">请仔细填写收货地址：</div>
			<div class="add-address-main">
				<div class="form-group">
					<label class="col-sm-3 control-label">收货人：</label>
					<div class="col-sm-9">
						<input type="text" class="form-control" id="realname">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">联系电话：</label>
					<div class="col-sm-9">
						<input type="text" class="form-control" id="mobile">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">地区：</label>
					<div class="col-sm-9">
						<select id="sel-provance" onChange="selectCity();" class="pull-left form-control" style="width:30%; margin-right:5%;">
							<option value="" selected="true">省/直辖市</option>
						</select>
						<select id="sel-city" onChange="selectcounty()" class="pull-left form-control" style="width:30%; margin-right:5%;">
							<option value="" selected="true">请选择</option>
						</select>
						<select id="sel-area" class="pull-left form-control" style="width:30%;">
							<option value="" selected="true">请选择</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">详细地址：</label>
					<div class="col-sm-9">
						<input type="text" class="form-control" id="address">
					</div>
				</div>
				<input type="hidden" id="addressid" value="" />
				<div class="form-group">
					<div class="col-sm-12">
						<button type="button" class="btn btn-danger" onclick="saveAddress()">保存</button>
						<button type="button" class="btn btn-default" onclick="addAddress();$('#addAddressPanel').hide();$('#addressid').val('');">取消</button>
					</div>
				</div>
			</div>
		</div>
	</div>
</form>

<script type="text/javascript" src="/addons/ewei_shopping/template/style/js/cascade.js"></script>

<script type="text/javascript">
	{if empty($addresses) && $_W['account']['level'] == ACCOUNT_SERVICE_VERIFY}
		wx.ready(function () {
			wx.openAddress({
				success : function(result) {
					//此处获取到地址信息，可做自己的业务操作
					$('#realname').val(result.userName);
					$('#mobile').val(result.telNumber);
					$('#sel-provance').val(result.provinceName);
					$('#sel-provance').trigger('change');
					$('#sel-city').val(result.cityName);
					$('#sel-city').trigger('change');
					$('#sel-area').val(result.countryName);
					$('#address').val(result.detailInfo);
				}
			});
		});
		{/if}
	var from='{$_GPC['from']}';
	var returnurl = '{$_GPC['returnurl']}';
	cascdeInit('','','');

	function saveAddress() {
		var realname = $('#realname').val();
		var mobile = $('#mobile').val();
		var province = $('#sel-provance').val();
		var city = $('#sel-city').val();
		var area = $('#sel-area').val();
		var address = $('#address').val();
		if (!realname) {
			alert('请输入您的真实姓名！');
			return false;
		}
		if (!mobile) {
			alert('请输入您的手机号码！');
			return false;
		}
		if (mobile.search(/^([0-9]{11})?$/) == -1) {
			alert('请输入正确的手机号码！');
			return false;
		}
		if (!address) {
			alert('请输入您的详细地址！');
			return false;
		}
		tip("正在处理数据...");
		$.post('{php echo $this->createMobileUrl('address')}', {
			'op' : 'post',
			'realname' : realname,
			'mobile' : mobile,
			'province' : province,
			'city' : city,
			'area' : area,
			'address' : address,
			'id' : $('#addressid').val()
		}, function(s) {
			tip_close();
			if (s.message != 0) {
				if (from == 'confirm') {
					if (returnurl != '') {
						window.location.href = decodeURIComponent(returnurl);;
						return;
					}
					window.location.href = '{php echo $this->createMobileUrl('confirm')}';
				}
				$("input [name='address']").attr('checked', false);
				var html='<div class="shopcart-main1 img-rounded" style="margin:0;padding:10px;margin-bottom:10px;cursor:pointer" onclick="editAddress(' + s.message +')">';
				var html1='<span>'+province+' '+city+' '+area+' '+''+address+' '+' <br/> ' +realname+', '+mobile+'</span>' +
					'<span style="float:right">' +
					'&nbsp;&nbsp;<a href="javascript:;" onclick="saveDefaultAddress('+ s.message +')">设置为默认收货地址</a></span>';
				var html2= '</div>';

				if ($('#address_'+s.message).get(0)) {
					$('#address_'+s.message).html(html1);
				} else {
					$(".address_item").removeClass("shopcart-main1").addClass("shopcart-main");
					$('#myaddress').append($(html + html1 + html2));
				}

				$('#realname').val('');
				$('#mobile').val('');
				$('#address').val('');
				cascdeInit('','','');
			}
			$('#addAddressPanel').hide();
			if (returnurl != '') {
				window.location.href = decodeURIComponent(returnurl);
				return;
			}
			window.location.href = '{php echo $this->createMobileUrl('confirm')}';
		}, 'json');
	}

	function addAddress() {
		$('#addAddressPanel').show();
		$('#realname').val('');
		$('#mobile').val('');
		$('#address').val('');
		cascdeInit('','','');
		$('#addressid').val('');
	}

	function saveDefaultAddress(e,id) {
		tip("正在处理数据...");
		$.post('{php echo $this->createMobileUrl('address', array('op' => 'default'))}', {
			'id' : id,
		}, function(s) {
			$(".address_item").removeClass("shopcart-main1").addClass("shopcart-main");
			$("#address_"+id).addClass("shopcart-main1");
			tip_close();
		}, 'json');
		e.stopPropagation() ;
	}

	function removeAddress(e,id){
		if(confirm('确认要删除此收货地址吗?')){
			 tip("正在处理数据...");
			$.post('{php echo $this->createMobileUrl('address', array('op' => 'remove'))}',
					{ 'id' : id,},
					function(s) {
						$("#address_"+id).remove();
						if(s.maxid!='0'){
							$(".address_item").removeClass("shopcart-main1").addClass("shopcart-main");
							$("#address_"+s.maxid).addClass("shopcart-main1");
						}
						tip_close();
					},
					'json');
			e.stopPropagation() ;
		}
	}

	var isdef = 0;

	function editAddress(id) {
		if (false && from == 'confirm'){
			tip("正在处理数据...");
			$.post('{php echo $this->createMobileUrl('address', array('op' => 'default'))}', {'id' : id}, function(s) {
				tip_close();
				if (returnurl != '') {
					window.location.href = returnurl;
					return;
				}
				window.location.href = '{php echo $this->createMobileUrl('confirm')}';
			});
			return;
		}

		tip("正在处理数据...");
		$.post('{php echo $this->createMobileUrl('address', array('op' => 'detail'))}', {'id' : id}, function(s){
			tip_close();
			if (s.message) {
				$('#addAddressPanel').show();
				$('#realname').val(s.message.username);
				$('#mobile').val(s.message.mobile);
				$('#address').val(s.message.address);
				cascdeInit(s.message.province,s.message.city,s.message.district);
				$('#addressid').val(s.message.id);
			}
		}, 'json');
	}
</script>

{template 'footer'}
{template 'footerbar'}